﻿using chihuopig.IService;
using System;
using System.Collections.Generic;
using System.Text;
using chihuopig.Model;
using System.Threading.Tasks;

namespace chihuopig.Service
{
    public class memberService : DBService, ImemberService
    {
        public long Add(Member model)
        {
            return Sqlclient.Insertable(model).ExecuteReturnBigIdentity();
        }

        public Task<int> delAll(long[] Id)
        {
            return Task.Factory.StartNew<int>(s => {
                string ls = string.Join(",", (long[])s);
                string sql = $"update  member set IsDelete='1' where id in ({ls})";
                return
                Sqlclient.Ado
               .ExecuteCommand(sql);
            }, Id);
        }

        public Task<int> Deleteable(long Id)
        {
            return Sqlclient
                      .Updateable<Member>()
                      .Where(c => c.Id == Id)
                      .ExecuteCommandAsync();
        }

        public Task<Member> EditAsync(long Id)
        {
            return Sqlclient.Queryable<Member>().Where(s => s.Id == Id).FirstAsync();
        }

        public Member FindOpenId(string OpenId)
        {
            return Sqlclient.Queryable<Member>().Where(s => s.openId == OpenId).Single();
        }

        public Task<double> GetPageTotal(int PageSize)
        {
            return Task.Factory.StartNew<double>(s =>
            {
                double size = Convert.ToDouble(s);
                double bnum = Sqlclient
                .Queryable<Member>()
                .OrderBy("Desc").Count() / size;
                //Math.Ceiling(bnum);
                return Math.Ceiling(bnum);

            }, PageSize);
        }

        public Task<long> Insertable(Member Model)
        {
            return Sqlclient.Insertable(Model).ExecuteReturnBigIdentityAsync();
        }

        public Task<int> Updateable(Member Model)
        {
            return Sqlclient.Updateable(Model).ExecuteCommandAsync();
        }
    }
}
